import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { LearningResourcesTypeEnum } from "./enum/learning-resources-type.enum";
import { Village } from './village.entity';
import { file } from "./about/file";


const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class LearningResources extends BaseEntity {
  @ApiProperty({
    description: '学习资源主键',
    required: false,
  })
  @PrimaryGeneratedColumn()
  id ?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.learningResources, {
    eager: true
  })
  village: Number | Village

  @ApiProperty({
    example: 1,
    description: '学习资源类型',
    type: Number,
    enum: LearningResourcesTypeEnum
  })
  @IsNumber({}, { always: true })
  @Column({
    type: "enum",
    enum: LearningResourcesTypeEnum,
  })
  type: LearningResourcesTypeEnum

  @ApiProperty({
    example: '1',
    description: '学习资源标题',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  title: string

  @ApiProperty({
    example: '1',
    description: '文件路径',
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  file: file []
}